package com.jianxiu.bean;

import java.util.Collections;
import java.util.List;

import com.jianxiu.config.SysConf;

/**
 * Created by tancw on 2017/3/23.
 */
public class Page<T> {
    public static final int PAGE_1ST;
    public static final int PAGE_SIZE;
    public static final int START;
    public static final int ALL = -1;
    private List<T> items = Collections.emptyList();
    private int pageNo;
    private int pageSize;
    private int total;
    private PageIndex pageindex;

    static {
        PAGE_1ST = Integer.parseInt(SysConf.get("page.pageList"));
        PAGE_SIZE = Integer.parseInt(SysConf.get("page.pageSize"));
        START = Integer.parseInt(SysConf.get("page.start"));
    }

    public static <T> Page<T> create(List<T> items, int total) {
        Page page = new Page();
        page.setItems(items);
        page.setTotal(total);
        return page;
    }

    public List<T> getItems() {
        return this.items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return this.pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getTotal() {
        return this.total;
    }

    public void setTotal(int total) {
        this.total = total;
        this.pageindex = PageIndex.getPageIndex(5L, this.pageNo + 1, (long) this.getPageCount());
    }

    public int getPageCount() {
        int pageCount = this.total / this.pageSize;
        if (this.total % this.pageSize > 0) {
            ++pageCount;
        }
        return pageCount;
    }

    public int getFirstPageNo() {
        return PAGE_1ST;
    }

    public int getLastPageNo() {
        return this.getPageCount() + PAGE_1ST - 1;
    }

    public int getItemSize() {
        return this.items.size();
    }

    public int getStart() {
        return (this.getPageNo() - this.getFirstPageNo()) * this.getPageSize() - START;
    }

    public int getLimit() {
        return this.getPageSize();
    }

    public PageIndex getPageindex() {
        return this.pageindex;
    }


    public Page() {
        this.pageNo = PAGE_1ST;
        this.pageSize = PAGE_SIZE;
        this.total = 0;
    }

    public static <T> Page<T> empty() {
        Page page = new Page();
        return page;
    }


    public Page(List<T> items, int pageNo, int paeSize, int total, PageIndex pageIndex) {
        this.items = items;
        this.pageNo = pageNo;
        this.pageSize = paeSize;
        this.total = total;
        this.pageindex = pageIndex;
    }
}
